package edu.zhku.ttms.service.impl;

import edu.zhku.ttms.domain.TrafficPoliceman;
import edu.zhku.ttms.mapper.TrafficPolicemanMapper;
import edu.zhku.ttms.service.TrafficPolicemanService;
import edu.zhku.ttms.utils.MybatisUtil;
import edu.zhku.ttms.utils.StringUtil;
import edu.zhku.ttms.utils.SystemConst;

import java.util.List;

/**
 * @author 泽
 * @since 2021/4/19 11:34
 */

public class
TrafficPolicemanServiceImpl implements TrafficPolicemanService {

    TrafficPolicemanMapper trafficPolicemanMapper = MybatisUtil.getMapper(TrafficPolicemanMapper.class);

    @Override
    public TrafficPoliceman getById(Long id) {
        return trafficPolicemanMapper.getById(id);
    }

    @Override
    public TrafficPoliceman getByCode(String policeCode) {
        return trafficPolicemanMapper.getByCode(policeCode);
    }

    @Override
    public List<TrafficPoliceman> getList() {
        return trafficPolicemanMapper.getList(null);
    }

    @Override
    public List<TrafficPoliceman> getList(String str) {
        return trafficPolicemanMapper.getList(str);
    }

    @Override
    public String save(TrafficPoliceman trafficPoliceman) {
        String policeCode = trafficPoliceman.getPoliceCode();
        if (StringUtil.isEmpty(policeCode)) {
            return "交警代号不能为空！";
        }
        if (StringUtil.isEmpty(trafficPoliceman.getName())) {
            return "交警姓名不能为空！";
        }
        if (StringUtil.isEmpty(trafficPoliceman.getBranch())) {
            return "交警所属分局不能为空！";
        }
        if (trafficPolicemanMapper.updateOne(trafficPoliceman) > 0) {
            return SystemConst.SUCCESS;
        }
        return "修改失败！";
    }

    @Override
    public String add(TrafficPoliceman trafficPoliceman) {
        String policeCode = trafficPoliceman.getPoliceCode();
        if (StringUtil.isEmpty(policeCode)) {
            return "交警代号不能为空！";
        }
        if (trafficPolicemanMapper.getByCode(policeCode) != null) {
            return "该交警代号已存在！";
        }
        if (StringUtil.isEmpty(trafficPoliceman.getName())) {
            return "交警姓名不能为空！";
        }
        if (StringUtil.isEmpty(trafficPoliceman.getBranch())) {
            return "交警所属分局不能为空！";
        }
        if (trafficPolicemanMapper.insertOne(trafficPoliceman) > 0) {
            return SystemConst.SUCCESS;
        }
        return "增加失败！";
    }

    @Override
    public int removeById(Long policeId) {
        return trafficPolicemanMapper.removeById(policeId);
    }
}
